Let us import the necessary Python Libraries for the Analysis of the Stock Market Performance
import pandas as pd
import yfinance as yf
from datetime import datetime
start_date = datetime.now() - pd.DateOffset(months=3)
end_date = datetime.now()
Now we will search and download for stock prices from few tech companies, I have taken stocks from Apple, Micorsoft, Meta and Google from the last 3 months.
tickers = ['AAPL', 'MSFT', 'META', 'GOOG']
df_list = []
for ticker in tickers:
data = yf.download(ticker, start = start_date, end = end_date)
df_list.append(data)
df = pd.concat(df_list, keys=tickers, names=['Ticker', 'Date'])
print(df.head())
[*********************100%***********************] 1 of 1 completed
[*********************100%***********************] 1 of 1 completed
[*********************100%***********************] 1 of 1 completed
[*********************100%***********************] 1 of 1 completed
Open High Low Close Adj Close \
Ticker Date
AAPL 2023-03-13 147.809998 153.139999 147.699997 150.470001 150.262161
2023-03-14 151.279999 153.399994 150.100006 152.589996 152.379227
2023-03-15 151.190002 153.250000 149.919998 152.990005 152.778687
2023-03-16 152.160004 156.460007 151.639999 155.850006 155.634735
2023-03-17 156.080002 156.740005 154.279999 155.000000 154.785904
Volume
Ticker Date
AAPL 2023-03-13 84457100
2023-03-14 73695900
2023-03-15 77167900
2023-03-16 76161100
2023-03-17 98944600
In the Dataset, we will set the Date Column as the Index Column
df = df.reset_index()
print(df.head())
Ticker Date Open High Low Close \
0 AAPL 2023-03-13 147.809998 153.139999 147.699997 150.470001
1 AAPL 2023-03-14 151.279999 153.399994 150.100006 152.589996
2 AAPL 2023-03-15 151.190002 153.250000 149.919998 152.990005
3 AAPL 2023-03-16 152.160004 156.460007 151.639999 155.850006
4 AAPL 2023-03-17 156.080002 156.740005 154.279999 155.000000
Adj Close Volume
0 150.262161 84457100
1 152.379227 73695900
2 152.778687 77167900
3 155.634735 76161100
4 154.785904 98944600
Now Let's look at the perfomance of the companies in the stock market:
import plotly.express as px
fig = px.line(df, x='Date',
y='Close',
color='Ticker',
title="Stock Market Porfomance from the last 3 Months")
fig.show()
Now Lets use the Area Chart and compare the perfomances of different Companies and Identify similarities or differences in their stock price movements
fig = px.area(df, x='Date', y='Close', color='Ticker', facet_col='Ticker',
labels={'Date':'Date', 'Close':'Closing Price', 'Ticker':'Company'},
title='Stock Prices of Apple, Micorsoft, Meta and Google')
fig.show()
df['MA10'] = df.groupby('Ticker')['Close'].rolling(window=10).mean().reset_index(0, drop=True)
df['MA20'] = df.groupby('Ticker')['Close'].rolling(window=20).mean().reset_index(0, drop=True)
# Table View
#for ticker, group in df.groupby('Ticker'):
# print(f'Moving Averages for {ticker}')
# print(group[['MA10', 'MA20']])
#Visualization of the Moving Average
for ticker, group in df.groupby('Ticker'):
fig = px.line(group, x = 'Date',
y = ['Close', 'MA10', 'MA20'],
title = f"{ticker} Moving Average")
fig.show()
The Above output we can observe that, When the MA10 crosses above MA20, it is considered a bullish signal indicating that the stock price will continue to rise. Conversly, when the MA10 crosses below the MA20, it is a bearish signal that the stock price will continue to fall.
df['Volatility'] = df.groupby('Ticker')['Close'].pct_change().rolling(window=10).std().reset_index(0, drop=True)
fig = px.line(df, x = 'Date',
y ='Volatility',
color ='Ticker',
title ='Volatility of All Companies')
fig.show()
#Create DataFrame with the Stock Pirces of the compnies corrlation you want to check
#For this Example, I took Meta and Google
meta = df.loc[df['Ticker'] == 'META', ['Date', 'Close']].rename(columns={'Close':'META'})
google = df.loc[df['Ticker'] == 'GOOG',['Date', 'Close']].rename(columns={'Close':'GOOG'})
df_corr = pd.merge(meta, google, on='Date')
#Visualization
fig = px.scatter(df_corr, x='META',
y='GOOG',
trendline='ols',
title='Correlation between Meta and Google')
fig.show()
The stock prices of Meta and Google exhibit a significant positive linear relationship, implying that when Meta's stock price rises, Google's stock price tends to rise as well.
This correlation suggests similarities between the companies, possibly influenced by industry trends, market conditions, or shared business associations.
Investors can consider this correlation as an opportunity to diversify their portfolio, as both stocks offer comparable potential returns and risks.